/**
 *  Copyright � 2010 Digia Plc
 *  Copyright � 2010 Nokia Corporation
 *
 *  All rights reserved.
 *
 *  Nokia and Nokia Connecting People are registered trademarks of
 *  Nokia Corporation.
 *  Java and all Java-based marks are trademarks or registered
 *  trademarks of
 *  Sun Microsystems, Inc. Other product and company names
 *  mentioned herein may be
 *  trademarks or trade names of their respective owners.
 *
 *
 *  Subject to the conditions below, you may, without charge:
 *
 *  �  Use, copy, modify and/or merge copies of this software and
 *     associated documentation files (the "Software")
 *
 *  �  Publish, distribute, sub-licence and/or sell new software
 *     derived from or incorporating the Software.
 *
 *
 *  This file, unmodified, shall be included with all copies or
 *  substantial portions
 *  of the Software that are distributed in source code form.
 *
 *  The Software cannot constitute the primary value of any new
 *  software derived
 *  from or incorporating the Software.
 *
 *  Any person dealing with the Software shall not misrepresent
 *  the source of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 *  KIND, EXPRESS OR IMPLIED,
 *  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import Qt 4.7
import "javascript/sauna.js" as SaunaEngine
import "javascript/sql.js" as HCS
import "../colibri/javascripts/functions.js" as Functions
import "../colibri"
import "styles"

Item {
    id: structure

    property bool timerVisible: false
    property CLStyle style: StyleHomeControlSystem {}
    property int spacing: 10;

    signal saunaTimed(int temperature, int length);


    /**
     * Get set sauna timimgs.
     */
    function getSaunaTimings(){
      saunaTimings.clear();
      for(var i=0;i < SaunaEngine.saunaTimes.length; i++) {
        saunaTimings.append({"date": SaunaEngine.saunaTimes[i].on.toString("dd.MM.yyyy HH:mm"), "length": SaunaEngine.saunaTimes[i].time})
      }
    }

    /**
     * Removes timing from SaunaTimes-array and updates
     * screen in UI which shows set sauna timimgs.
     * @param index Timing to remove.
     */
    function removeTiming(index) {
        SaunaEngine.saunaTimes.splice(index,1);
        getSaunaTimings();
    }


    Component.onCompleted: {
        getSaunaTimings()
        HCS.setDefaults();
    }

    width: 640
    height: 424

    MouseArea {
        anchors.fill: parent;
    }

    Rectangle{
        width:structure.width*0.955
        height:structure.height*0.924
        color:style.selectionColor
        anchors.centerIn:parent
        radius:6

    }

    Image {
        source: "images/control_bg_trans_420x275_png.png";
        anchors.fill: parent;
        smooth: true
        opacity:0.7
    }


    Timer {
        interval: 1000; running: true; repeat: true
        onTriggered: {
            var saunaTiming = SaunaEngine.isSaunaOn();
            if(saunaTiming != null) {
                structure.saunaTimed(saunaTiming.setTemperature,
                    SaunaEngine.saunaTimeLeft(saunaTiming.off)/60/1000);
            }
            if(SaunaEngine.deleteOldTimings()) getSaunaTimings();
        }
    }

    Row {
        x: structure.width*0.02;
        y: structure.height*0.05;

        Column {
            width: structure.width*0.42
            Text {
                x: structure.width*0.02
                text: "Scheduled sauna times:"
                font.pointSize: 0.001+structure.height*0.035
                color: structure.style.textColor
            }

            Component {
                id: timingsDelegate
                Row {
                    x: structure.width*0.02
                    spacing: 5
                    width: structure.width*0.32
                    Text { text: date; font.pointSize: 0.001+structure.height*0.025; anchors.verticalCenter: parent.verticalCenter; color: structure.style.textColor}
                    Text { text: length + " min"; font.pointSize: 0.001+structure.height*0.025; anchors.verticalCenter: parent.verticalCenter; color: structure.style.textColor}
                    CLButton{
                        gradientWhenDefault: structure.style.gradientWhenDefault;
                        gradientWhenHovered: structure.style.gradientWhenHovered;
                        gradientWhenPressed: structure.style.gradientWhenPressed;
                        colorWhenDefault: structure.style.colorWhenDefault;
                        colorWhenHovered: structure.style.colorWhenHovered;
                        colorWhenPressed: structure.style.colorWhenPressed;
                        borderWidth: -1
                        text: "del"; fontSize: 0.001+structure.height*0.025;
                        onClicked: removeTiming(index);
                        textColor: structure.style.textColor;
                        anchors.right: parent.right
                        smooth: true
                    }
                }
            }

            Rectangle {
                width: structure.width*0.35;
                height: structure.height*0.85;
                color: "transparent"
                radius: 10//Functions.countRadius(styleHCS.roundness,width,height,0,1);
                ListView {
                    width: parent.width
                    height: parent.height
                    model: saunaTimings
                    delegate: timingsDelegate
                    clip: true
                }
            }

            ListModel {
                id: saunaTimings;
            }
        }



        Column {
            y: structure.height*0.12
            spacing: structure.height*0.05
            Text {text: "Temperature: " + setTemperature.value + " \u00BAC"; font.pointSize: 0.001+structure.height*0.06; color: structure.style.textColor}
            CLSlider{
                id: setTemperature;
                height: structure.height*0.11;
                width: structure.width*0.52;
                style: structure.style;
                minimum: 30
                maximum: 120
                Component.onCompleted: setValue(70);
                borderColor: "white";
                borderWidth: 3
            }

            Text {
                text: "Duration: " + (parseInt(setTime.value/60) + " h" + " " +(setTime.value % 60)) +  " min"; font.pointSize: 0.001+structure.height*0.06
                color: structure.style.textColor
            }

            CLSlider{
                id: setTime;
                minimum: 0;
                maximum: 180;
                height: structure.height*0.11;
                width: structure.width*0.52;
                style: structure.style; borderColor: "white";
                borderWidth: 3;
                Component.onCompleted: setValue(30);
            }


            Row {
                spacing: 4

                CLButton {
                    id: timerIcon;
                    style: structure.style
                     text: ""
                     width: structure.width*0.11
                     height: structure.height*0.16
                     backgroundImage: Image{source: "images/calendar_100x100.png"; width: structure.width*0.1; height: structure.height*0.15; smooth:true}
                     onClicked: {
                        timer.state = "visible"
                        timer.visible = true
                     }
                }
                Column {
                    Text {
                        text: datePicker.selected
                        font.pointSize: 0.001+structure.height*0.047
                        color: structure.style.textColor
                     }
                     Text {
                        text: clock.hours + ":" + clock.minutes
                        font.pointSize: 0.001+structure.height*0.047
                        color: structure.style.textColor
                     }
                     width: structure.width*0.2
                }

                CLButton{
                    style: structure.style;
                    text: "Set Timer";
                    anchors.verticalCenter: parent.verticalCenter;
                    fontSize: 0.001+structure.height*0.045;
                    onClicked: {
                        if(setTime.value > 0) SaunaEngine.newSaunaOn(datePicker.selected,clock.hours,clock.minutes,setTime.value,setTemperature.value);
                        getSaunaTimings();
                    }
                }
            }
        }
    }

    Rectangle {
        id: timer
        x: structure.width+200
        state: "unvisible"
        anchors.verticalCenter: structure.verticalCenter;
        width: structure.width*0.94;
        height: structure.height*0.9;
        visible: false
        radius: 10
        z: 1
        color: structure.style.bgColor;
        border.color: structure.style.borderColor;

        MouseArea {
            anchors.fill: parent
        }


        CLDatePicker {
             id: datePicker;
             style: structure.style;
             showSelectedDate: false;
             width: structure.width*0.47;
             height: structure.height*0.85;
             pastDaysDisabled: true
             anchors.top: parent.top
             anchors.topMargin: structure.height*0.02
             anchors.left: parent.left
             anchors.leftMargin: structure.width*0.02
             onClicked: {
               if (SaunaEngine.isToday(datePicker.selected)) clock.pastClockTimesDisabled = true
               else clock.pastClockTimesDisabled = false
             }
             smooth: true
             hoveredStateOn: false
        }

        ClockTimer {
            id: clock
            style: structure.style
            textColor: textColor
            width: structure.width*0.4
            height: structure.height*0.2
            anchors.right: parent.right
            anchors.rightMargin: structure.width*0.015
            anchors.top: parent.top
            color: "transparent"
            anchors.topMargin: structure.height*0.02
            fontSize: structure.height*0.11
        }

        CLButton {
            text: "OK";
            style: structure.style;
            anchors.bottom: parent.bottom;
            anchors.bottomMargin: structure.width*0.01;
            anchors.right: parent.right
            anchors.rightMargin: structure.width*0.01
            fontSize: structure.height*0.08;
            onClicked: {
                timer.state = "unvisible"
            }
        }

        states: [
        State {
            name: "visible"; //when: timerVisible
        }, State {
            name: "unvisible"
        }
        ]

        transitions: [
        Transition {
            from: "unvisible"
            to: "visible"
            PropertyAnimation{target: timer; property: "x"; to: structure.width*0.03; duration: 500}
        },
        Transition {
            from: "visible"
            to: "unvisible"
            PropertyAnimation{target: timer; property: "x"; to: structure.width + 200; duration: 500}
            PropertyAnimation{target: timer; property: "visible"; to: "false"; duration: 500}
        }
        ]
    }
}

